package feiyanren.com;

public class A02_Demo2 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int target = 5;
        int index = find(arr,target);
        System.out.println(index);
    }
    public static int find(int[] arr,int target){
        int left = 0;
        int right = arr.length - 1;
        while(true){
            if(left > right){
                return -1;
            }
            int mid = (left + right) / 2;
            if(arr[mid] == target){
                return mid;
            }
            else if(arr[mid] > target){
                right = mid - 1;
            }
            else{
                left = mid + 1;
            }
        }
    }
}
